package com.vogue.supplymall.entmanagement.domain.provider;

import com.vogue.ssm.database.provider.BaseSqlProvider;
import com.vogue.ssm.database.util.DealStatus;
import com.vogue.supplymall.common.domain.dto.LicenceShopSearchInfo;
import org.apache.commons.lang3.StringUtils;

/**
 * The type Current shop sql provider.
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-04-16
 */
public class LicenceShopListSqlProvider extends BaseSqlProvider {

    /**
     * Find string.
     * 当前店铺和续期店铺的检索sql
     * applytype区分当前或者续期，{@linkplain  LicenceShopSearchInfo}
     *
     * @param licenceShopSearchInfo the licence shop search info
     * @return the string
     */
    public String find(LicenceShopSearchInfo licenceShopSearchInfo) {
        StringBuilder builder = new StringBuilder();
        builder.append(" SELECT ")
                .append(" @rownum :=@rownum + 1 AS rownum, ")
                .append(" s.id sid, ")
                .append(" s.entname, ")
                .append(" s.`name`, ")
                .append(" la.id la_id, ")
                .append(" la.applystatue, ")
                .append(" la.applytype, ")
                .append(" COUNT(lad.id) licence_count ");
        builder.append(" FROM ")
                .append(" licence_apply la ")
                .append(" INNER JOIN shop s ON (s.id = la.shopid) ")
                .append(" INNER JOIN licence_apply_detail lad ON (la.id = lad.applyid) ")
                .append(" LEFT JOIN licence_scopeofbusiness ls ON (la.scopeid = ls.id), ")
                .append(" (SELECT @rownum := 0) AS it ");
        builder.append(" WHERE ")
                .append(" la.applytype = #{applytype} ")
                .append(" AND s.syscodeid = #{syscodeid} ");
        if (StringUtils.isNotEmpty(licenceShopSearchInfo.getContent())) {
            builder.append(" AND s.name like CONCAT('%',#{content},'%') ");
        }
        if (null != licenceShopSearchInfo.getApplystatue()) {
            builder.append(" AND la.applystatue = #{applystatue} ");
        }
        builder.append(" GROUP BY s.id ");
        DealStatus.kacha();
        return builder.toString();
//        return DealStatus.formatShadeSql(builder.toString());
    }
}
